﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MySql.Data.MySqlClient;
using LdExpress;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
    public string strCondition = "";
    public string strError = "";
    public string strMess = "";
    public int nPermissions = 0;
    public int ctrrow = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["SuperAdmin"] != null && int.Parse(Session["SuperAdmin"].ToString()) > 0)
        {
            if (Session["SuperAdmin"] != "") nPermissions = int.Parse(Session["SuperAdmin"].ToString());
        }
        else
            Response.Redirect("Login.aspx");
        if (!IsPostBack)
        {
        }
    }
    protected void btnBack_Click1(object sender, EventArgs e)
    {
        Response.Redirect("AdminUserList.aspx");
    }
    protected void SubmitImport_Click(object sender, System.EventArgs e)
    {
        CustomerRelation obj = null;
        HttpPostedFile inpFile = null;
        FileInfo FICSV = null;
        DataColumnCollection tableColumns = null;
        bool isExcludeSum = false;
        if (cbExcludedSummary.Checked) isExcludeSum = true;
        string fileName = Request.Form["txtfileUpLoad"];

        if (Request.Files["txtfileUpLoad"] != null)
        {
            inpFile = Request.Files["txtfileUpLoad"];
        }
        if (txtfileUpLoad.PostedFile.FileName != "")
        {
            FICSV = new FileInfo(txtfileUpLoad.PostedFile.FileName);
            try
            {
                ImportDB.SaveFileToServer(fileName, inpFile);

                DataSet dsCSV = new DataSet();
                dsCSV = ImportDB.ConnectFile(FICSV);

                try
                {
                    tableColumns = dsCSV.Tables[0].Columns;
                }
                catch (Exception ex)
                {
                    throw ex;
                }

                DataRowCollection tableRows = dsCSV.Tables[0].Rows;
                long curUserID = -1;
                long curOwnerID = -1;
                int nSuperPermissions = 0;
                //if (Session["superadmin"] != "") nSuperPermissions = int.Parse(Session["superadmin"].ToString());
                try
                {
                    curUserID = long.Parse(Session["logined_id"].ToString());
                    curOwnerID = long.Parse(Session["logined_id"].ToString());
                }
                catch { curUserID = -1; }
                if (Request.QueryString["custstore"] != null && Request.QueryString["custstore"].ToString() == "1")
                    curUserID = -1;
                //if (nSuperPermissions > 0) curUserID = -1;
                //string strNameDuplicate = "";                
                foreach (DataRow row in tableRows)
                {
                    obj = new CustomerRelation();
                    object[] rowItems = row.ItemArray;

                    int ctrColumn = 0;
                    if (Request.QueryString["custtype"] != null && Request.QueryString["custtype"].ToString() == "2")
                    {
                        if (rowItems[3].ToString().Replace("'", "").Trim().Length > 0)
                        {
                            ctrrow++;
                            //Check to Sum vol
                            double nCont20 = 0, nCont40 = 0;
                            int nVolsExt = 0;
                            double dTemp;
                            int iTemp;
                            obj.Code = rowItems[2].ToString().Replace("'", "").Trim();
                            if (obj.Code.Length == 9 || obj.Code.Length == 12) obj.Code = "0" + obj.Code;
                            obj.Active_Type = CommonFunctions.GetActiveTypeByCodeType(rowItems[11].ToString());
                            try
                            {
                                obj.LastUpdate = DateTime.Parse(rowItems[0].ToString().Trim());
                            }
                            catch { }
                            long tempID = CustomerRelation.GetCustIDToImportActiveCust(obj.Code, rowItems[1].ToString(), rowItems[8].ToString()
                                , rowItems[4].ToString(), rowItems[5].ToString()
                                , obj.LastUpdate, obj.Active_Type, rowItems[12].ToString(), rowItems[14].ToString()
                                , out nCont20, out nCont40, out nVolsExt, GV.cn, GV.trans);
                            if (tempID > 0) // sum vol cont 20, 40 when the same pol, pld, code
                            {
                                if (isExcludeSum) continue;
                                obj.ID = tempID;
                                obj.Load(GV.cn, GV.trans);

                                double.TryParse(rowItems[6].ToString().Replace("'", "").Replace(",", ".").Trim(), out dTemp);
                                dTemp += nCont20;
                                obj.Vols = System.Math.Round(dTemp).ToString();
                                double.TryParse(rowItems[7].ToString().Replace("'", "").Replace(",", ".").Trim(), out dTemp);
                                dTemp += nCont40;
                                obj.Report = System.Math.Round(dTemp).ToString();
                                obj.Address = CommonFunctions.GetProvinceByTaxCode(obj.Code);
                                //obj.Active_Type = CommonFunctions.GetActiveTypeByCodeType(rowItems[11].ToString());
                                //Vols Ext                                
                                //int.TryParse(rowItems[12].ToString().Replace("'", "").Replace(",", ".").Trim(), out iTemp);
                                //iTemp += nVolsExt;
                                //obj.Vols_Ext = iTemp;
                            }
                            else
                            {
                                obj.Pol = rowItems[1].ToString().Replace("'", "").Trim();
                                obj.Name = rowItems[3].ToString().Replace("'", "").Trim();
                                obj.Address = CommonFunctions.GetProvinceByTaxCode(obj.Code);
                                obj.HsGoods = rowItems[4].ToString().Replace("'", "").Trim();
                                obj.HsGoodsEx = rowItems[5].ToString().Replace("'", "").Trim();

                                double.TryParse(rowItems[6].ToString().Replace("'", "").Replace(",", ".").Trim(), out dTemp);
                                obj.Vols = System.Math.Round(dTemp).ToString();
                                double.TryParse(rowItems[7].ToString().Replace("'", "").Replace(",", ".").Trim(), out dTemp);
                                obj.Report = System.Math.Round(dTemp).ToString();
                                obj.Routers = rowItems[8].ToString().Replace("'", "").Trim();
                                obj.Goods = rowItems[9].ToString().Replace("'", "").Trim();
                                obj.GoodsEx = rowItems[10].ToString().Replace("'", "").Trim();
                                //Vols Other
                                //obj.Vols_Type = rowItems[11].ToString().Replace("'", "").Trim().ToLower();
                                //int.TryParse(rowItems[12].ToString().Replace("'", "").Replace(",", ".").Trim(), out iTemp);
                                //obj.Vols_Ext = iTemp;
                                //obj.Routers_Ext = rowItems[13].ToString().Replace("'", "").Trim();                                
                                obj.Active_Dlvry = rowItems[12].ToString();
                                try
                                {
                                    obj.Active_Value = float.Parse(rowItems[13].ToString());
                                }
                                catch { }
                                obj.Active_Cnee = rowItems[14].ToString();
                                Countries objCountry = new Countries();
                                objCountry.Load(rowItems[15].ToString().Replace("'", "").Trim(), GV.cn, GV.trans);
                                obj.Country = objCountry.CountryName;
                                if (objCountry.CountryName.Length == 0)
                                    obj.Country = rowItems[15].ToString().Replace("'", "").Trim();
                            }
                            obj.Archive = 2;
                            if (Request.QueryString["custstore"] != null && Request.QueryString["custstore"].ToString() == "1")
                                obj.ListType = -1;
                            obj.UserID = curUserID;
                            obj.Owner = curOwnerID;
                            ctrColumn++;

                            //if (CustomerRelation.IsUniqueNameActiveExisting(obj.Name, obj.ID, GV.cn, GV.trans))
                            //{
                            //    strMess += "Dữ liệu dòng thứ: " + ctrrow + " đã có<br>";
                            //    obj.Duplicate = 1;
                            //}
                            obj.Save(GV.cn, GV.trans);
                        }
                    }
                    else
                    {
                        if (rowItems[5].ToString().Replace("'", "").Trim().Length > 0)
                        {
                            ctrrow++;
                            obj.HsGoods = rowItems[1].ToString().Replace("'", "").Trim();
                            obj.Goods = rowItems[2].ToString().Replace("'", "").Trim();
                            obj.HsGoodsEx = rowItems[3].ToString().Replace("'", "").Trim();
                            obj.GoodsEx = rowItems[4].ToString().Replace("'", "").Trim();
                            obj.Code = rowItems[5].ToString().Replace("'", "").Trim();
                            obj.Name = rowItems[6].ToString().Replace("'", "").Trim();
                            obj.Address = rowItems[7].ToString().Replace("'", "").Trim();
                            if (obj.Address.Trim().Length == 0)
                                obj.Address = CommonFunctions.GetProvinceByTaxCode(obj.Code);
                            obj.Tel = rowItems[8].ToString().Replace("'", "").Trim();
                            obj.Mobile = rowItems[9].ToString().Replace("'", "").Trim();
                            obj.Email = rowItems[10].ToString().Replace("'", "").Trim();
                            obj.Pic = rowItems[11].ToString().Replace("'", "").Trim();
                            obj.Position = rowItems[12].ToString().Replace("'", "").Trim();
                            obj.Social = rowItems[13].ToString().Replace("'", "").Trim();
                            obj.Pol = rowItems[14].ToString().Replace("'", "").Trim();
                            obj.Routers = rowItems[15].ToString().Replace("'", "").Trim();
                            obj.Vols = rowItems[16].ToString().Replace("'", "").Trim();
                            obj.Report = rowItems[17].ToString().Replace("'", "").Trim();
                            //if (Request.QueryString["custtype"] != null && Request.QueryString["custtype"].ToString() == "2")
                            //    obj.Archive = 2;
                            obj.ListType = 3;
                            if (Request.QueryString["custstore"] != null && Request.QueryString["custstore"].ToString() == "1")
                                obj.ListType = -1;
                            obj.UserID = curUserID;
                            obj.Owner = curOwnerID;
                            obj.LastUpdate = DateTime.Now;
                            ctrColumn++;

                            //if (CustomerRelation.IsCodeExisting(obj.Code, obj.ID, GV.cn, GV.trans))
                            //{
                            //    strMess += "Dữ liệu dòng thứ: " + ctrrow + " đã có<br>";
                            //    obj.Duplicate = 1;
                            //}
                            obj.Save(GV.cn, GV.trans);
                        }

                    }
                }
                //Response.Write(strNameDuplicate);
                strMess += "Đã có " + ctrrow.ToString() + " được import vào hệ thống!";
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());

            }
        }
        else
        {
            strMess = "Bạn phải chọn file import!";
            return;
        }
    }
}

